package com.hangzhou.xyj.ui.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.BookModel;
import com.hangzhou.xyj.model.BookPageInfoModel;
import com.hangzhou.xyj.net.handler.GetBookListHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetBookListReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.BookHeadersAdapter;
import com.hangzhou.xyj.ui.widget.DividerItemDecoration;
import com.hangzhou.xyj.ui.widget.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/8.
 * 课本
 */
public class BookActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private StickyRecyclerHeadersDecoration headersDecoration;
    private BookHeadersAdapter mAdapter;

    @Override
    public void initTop() {
        enableTitle(true, "课本");
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_book);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_book);
    }

    @Override
    public void initDatas() {
        //设置水平布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new BookHeadersAdapter(mContext, new ArrayList<BookModel>()));
        // Add the sticky headers decoration
        headersDecoration = new StickyRecyclerHeadersDecoration(mAdapter);
        mRecyclerView.addItemDecoration(headersDecoration);
        //向服务器请求数据
        GetBookListReq getBookListReq = new GetBookListReq(15, 1);//TODO 要改的
        HttpLoader.getInstance().sendRequest(new GetBookListHandler(getBookListReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                //请求到数据之后
                List<BookModel> books = ((BookPageInfoModel) model).getBookList();
                mAdapter.update(books);
            }
        }, 0));

        //设置水平分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST
                , R.drawable.line_divider_one));

        mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                headersDecoration.invalidateHeaders();
            }
        });
    }

    @Override
    public void initListeners() {

    }
}
